
from rest_framework import viewsets


from MirrorCore.models.Content import Article
from MirrorCore.models.User import User
from MirrorCore.permissions.permisssion import IsStaffOrReadOnly
from MirrorCore.serializers.ArticleSerializer import ArticleSerializer
from MirrorCore.serializers.UserSerializer import UserSerializer
from MirrorCore.serializers.BadgeSeriralizer import ArticleBadgeSerializer
from MirrorCore.views.utils.BadgeList import update_badge_list


class ArticleViewSet(viewsets.ModelViewSet):
    serializer_class = ArticleSerializer
    permission_classes = [IsStaffOrReadOnly]

    def get_queryset(self):
        """
        非管理员返回 仅展示的文章
        :return:
        """
        if self.request.user.is_staff:
            return Article.objects.all().order_by("-id")
        return Article.objects.filter(is_active=True).order_by("-id")


    def list(self, request, *args, **kwargs):
        # if request.user and request.user.is_authenticated:
        # self.queryset = Article.objects.filter(title="未命名标题")
        # else:
        # data = request.GET
        #
        # # print(**data)
        # self.queryset = Article.objects.all().order_by('-create_time')
        # if data.get("title"):
        #     self.queryset = self.queryset.filter(title=data.get('title')).order_by('-create_time')
        # if data.get("author"):
        #     self.queryset = self.queryset.filter()
        return super().list(request, *args, **kwargs)

    def update(self, request, *args, **kwargs):

        data = request.data
        """ 单独处理 badges 的修改 """
        if data.get('badges'):
            update_badge_list(model=self.serializer_class.Meta.model.badges.field.model
                          , name='article', pk=kwargs['pk'],
                          serializer=ArticleBadgeSerializer,
                          data=data.get("badges")
                          )

        """单独处理 collection的修改"""

        return super().update(request, *args, **kwargs)

    def destroy(self, request, *args, **kwargs):

        """ 单独处理 badges 的修改 """
        update_badge_list(model=self.serializer_class.Meta.model.badges.field.model
                          , name='article', pk=kwargs['pk'],
                          serializer=ArticleBadgeSerializer,
                          data=[]
                          )
        return super().destroy(request, *args, **kwargs)

    def create(self, request, *args, **kwargs):

        """
            Todo 该函数未返回一同创建的badges 不影响，跳转后重新请求数据
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        # request.data['author'] = 1
        data = super().create(request, *args, **kwargs)

        print( data.data['id'] )



        """ 单独处理 badges 的修改 """
        update_badge_list(model=self.serializer_class.Meta.model.badges.field.model
                          , name='article', pk=data.data['id'],
                          serializer=ArticleBadgeSerializer,
                          data=request.data.get("badges")
                          )

        return data

class AuthorViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    # permission_classes = [permissions.IsAuthenticated]
